package io.bsly.modules.app.service.localmaster.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.Query;
import io.bsly.modules.app.dao.localDao.DyCouponDao;
import io.bsly.modules.app.entity.localEntity.DyCouponEntity;
import io.bsly.modules.app.entity.localEntity.DyProductEntity;
import io.bsly.modules.app.pojo.CouponVo;
import io.bsly.modules.app.service.localmaster.DyCouponService;
import io.bsly.modules.app.service.localmaster.DyProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("dyCouponService")
public class DyCouponServiceImpl extends ServiceImpl<DyCouponDao, DyCouponEntity> implements DyCouponService {
    @Autowired
    private DyProductService dyProductService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<DyCouponEntity> lambda = new QueryWrapper<DyCouponEntity>().lambda();
        if (params.containsKey("status")&& !StringUtils.isEmpty(params.get("status"))){
            String[] statusArr = params.get("status").toString().split(",");
            List<String> statusList = Arrays.asList(statusArr);
            lambda.in(DyCouponEntity::getStatus,statusList);
        }

        if (params.containsKey("userId") && !StringUtils.isEmpty(params.get("userId")))
            lambda.eq(DyCouponEntity::getUserId,params.get("userId"));//指定用户ID

        if (params.containsKey("username")  && !StringUtils.isEmpty(params.get("username"))){
            lambda.eq(DyCouponEntity::getUsername,params.get("username"));//指定用户名
        }
        if (params.containsKey("dyOrderCode")  && !StringUtils.isEmpty(params.get("dyOrderCode")) ){
            lambda.eq(DyCouponEntity::getDyOrderCode,params.get("dyOrderCode"));
        }
        if (params.containsKey("createTime")  && !StringUtils.isEmpty(params.get("createTime")) ){
            lambda.apply("date_format(create_time,'%Y-%m-%d %H:%i:%s') = date_format('"+params.get("createTime").toString()+"','%Y-%m-%d %H:%i:%s')");
        }
        if (params.containsKey("createDate") && !StringUtils.isEmpty(params.get("createDate")) ){
            lambda.apply("date_format(create_time,'%Y-%m-%d') = date_format('"+params.get("createDate").toString()+"','%Y-%m-%d')");
        }
        if (params.containsKey("productName") && !StringUtils.isEmpty(params.get("productName")) ){
            lambda.eq(DyCouponEntity::getProductName,params.get("productName"));
        }
        if (params.containsKey("productId")  && !StringUtils.isEmpty(params.get("productId")) ){
            lambda.eq(DyCouponEntity::getProductId,params.get("productId"));
        }
        IPage<DyCouponEntity> page = this.page( new Query<DyCouponEntity>().getPage(params), lambda );

        List<DyCouponEntity> listCoupons = page.getRecords();
        List<Integer> listProductId = listCoupons.stream().map(one -> one.getProductId()).collect(Collectors.toList());
         List<DyProductEntity> listProductEntities =null;
        if (!CollectionUtils.isEmpty(listProductId))
           listProductEntities = dyProductService.listByIds(listProductId);

        final List<DyProductEntity>  listProduct= listProductEntities;
        List<CouponVo> CouponVos = listCoupons.stream().map(couponItem -> {
            CouponVo couponVo = new CouponVo();
            List<DyProductEntity> collectProduct = listProduct.stream().filter(one -> one.getId() == couponItem.getProductId()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collectProduct)) {
                DyProductEntity productEntity = collectProduct.get(0);
                couponVo.setTotalNightCount(productEntity.getNightCount());
                BeanUtils.copyProperties(productEntity, couponVo);
                couponVo.setProductName(productEntity.getName());
                couponVo.setProductCode(productEntity.getCode());
            }
            couponVo.setCid(couponItem.getId());
            couponVo.setCurrentNightCount(couponItem.getNightCount());
            BeanUtils.copyProperties(couponItem, couponVo);
            return couponVo;
        }).collect(Collectors.toList());
        return new PageUtils(CouponVos,(int)page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
    }

}